/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jul.giftmanager.ui;

import com.alee.extended.date.WebDateField;
import com.alee.laf.combobox.WebComboBox;
import com.alee.laf.label.WebLabel;
import com.alee.laf.text.WebTextField;
import com.jul.giftmanager.obj.Gender;
import com.jul.giftmanager.obj.Person;
import java.awt.event.ActionEvent;
import com.jul.giftmanager.obj.Relation;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

/**
 *
 * @author user
 */
public class AddPersonInfoPanel extends JPanel
{
    private WebTextField nameField;
    private WebComboBox typeOfRelationCombo;
    private WebDateField birthdayField;
    private WebComboBox genderCombo;
    private WebLabel nameLbl;
    private WebLabel birthdayLbl;
    private WebLabel genderLbl;
    private WebLabel typeOfRelationLbl;
    private Person personToAdd;
    private ButtonsPanel buttonsPanel;

    public AddPersonInfoPanel()
    {
        nameField = new WebTextField();
        typeOfRelationCombo = new WebComboBox();
        birthdayField = new WebDateField();
        genderCombo = new WebComboBox();
        
        nameLbl = new WebLabel();
        birthdayLbl = new WebLabel();
        genderLbl = new WebLabel();
        typeOfRelationLbl = new WebLabel();
        buttonsPanel = new ButtonsPanel(ButtonsPanel.ButtonsPanelState.OK_BTN, ButtonsPanel.ButtonsPanelState.CANCEL_BTN);
        init();
    }
    
   private void init(){
       
       nameLbl.setText("Name: ");
       birthdayLbl.setText("Birthday date: ");
       genderLbl.setText("Gender: ");
       typeOfRelationLbl.setText("Relation to you: ");
       
       fillComboBoxes();
              
       setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
       Box nameBox = Box.createHorizontalBox();
       nameBox.add(nameLbl);
       nameBox.add(Box.createHorizontalStrut(5));
       nameBox.add(nameField);
       
       Box dbAndGenderBox = Box.createHorizontalBox();
       dbAndGenderBox.add(birthdayLbl);
       dbAndGenderBox.add(Box.createHorizontalStrut(5));
       dbAndGenderBox.add(birthdayField);
       dbAndGenderBox.add(Box.createHorizontalStrut(15));
       dbAndGenderBox.add(genderLbl);
       dbAndGenderBox.add(Box.createHorizontalStrut(5));
       dbAndGenderBox.add(genderCombo);
       
       Box relationBox = Box.createHorizontalBox();
       relationBox.add(typeOfRelationLbl);
       relationBox.add(Box.createHorizontalStrut(5));
       relationBox.add(typeOfRelationCombo);
       
       add(nameBox);
       add(dbAndGenderBox);
       add(relationBox);
       add(buttonsPanel);
       

   }
    
   
   public void setActionForButtons(Action saveAction, Action cancelAction){
       buttonsPanel.setOkBtnAction(saveAction);
       buttonsPanel.setCancelBtnAction(cancelAction);
   }
   
      private void fillComboBoxes(){
       Gender[] genderValues = Gender.values();
       for (int i = 0; i < genderValues.length; i++)
       {
           genderCombo.addItem(genderValues[i]);
       }
       
       Relation[] relationValues = Relation.values();
       for (int i = 0; i < relationValues.length; i++)
       {
           typeOfRelationCombo.addItem(relationValues[i]);
       }
   }

   private void fillInfo(Person person){
       nameField.setText(person.getPersonName());
       birthdayField.setDate(person.getBirthDate());
       genderCombo.setSelectedItem(person.getGender());
       typeOfRelationCombo.setSelectedItem(person.getRelation());
   }
   
       public void setPersonToAdd(Person personToAdd)
    {
        this.personToAdd = personToAdd;
        fillInfo(this.personToAdd);
    }
   
   
   public Person getPerson(){
       personToAdd.setPersonName(nameField.getText());
       personToAdd.setBirthDate(birthdayField.getDate());
       personToAdd.setGender(Gender.MALE);
       personToAdd.setRelation(Relation.CLOSE_FRIEND);
       return personToAdd;
   }
  
   
    public static void main(String[] args)
    {
        BasicFrame frame = new BasicFrame();
        AddPersonInfoPanel panel = new AddPersonInfoPanel();
        frame.getContentPane().add(panel);
        frame.getContentPane().add(new AddGiftPanel());
    }
}
